package ui;

import java.awt.BorderLayout;
import java.awt.FlowLayout;

import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.border.EmptyBorder;
import javax.swing.JLabel;
import javax.swing.JTextField;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

public class ConnectDatabase extends JDialog {

	private final JPanel contentPanel = new JPanel();
	private JTextField txtUsername;
	private JPasswordField txtPassword;
	private JTextField txtHostname;
	private JTextField txtPort;
	private JTextField txtServiceName;
	
	public String getHost() {
		return txtHostname.getText();
	}
	
	public String getUsername() {
		return txtUsername.getText();
	}
	
	public String getPassword() {
		return txtPassword.getText();
	}
	
	public String getServiceName() {
		return txtServiceName.getText();
	}
	
	public String getPort() {
		return txtPort.getText();
	}
	
	public ConnectDatabase(JFrame frame) {
		super(frame, true);
		setTitle("Kết nối CSDL");
//		setDefaultCloseOperation(HIDE_ON_CLOSE);
		setBounds(100, 100, 450, 241);
		getContentPane().setLayout(null);
		contentPanel.setBounds(0, 0, 434, 168);
		contentPanel.setBorder(new EmptyBorder(5, 5, 5, 5));
		getContentPane().add(contentPanel);
		contentPanel.setLayout(null);
		{
			JLabel lblUsername = new JLabel("Username");
			lblUsername.setBounds(10, 11, 80, 20);
			contentPanel.add(lblUsername);
		}
		{
			JLabel lblPassword = new JLabel("Password");
			lblPassword.setBounds(10, 42, 80, 20);
			contentPanel.add(lblPassword);
		}
		{
			JLabel lblHostname = new JLabel("Hostname");
			lblHostname.setBounds(10, 73, 80, 20);
			contentPanel.add(lblHostname);
		}
		{
			JLabel lblPort = new JLabel("Port");
			lblPort.setBounds(10, 104, 80, 20);
			contentPanel.add(lblPort);
		}
		{
			JLabel lblServiceName = new JLabel("Service Name");
			lblServiceName.setBounds(10, 135, 80, 20);
			contentPanel.add(lblServiceName);
		}
		
		txtUsername = new JTextField();
		txtUsername.setText("test_1");
		txtUsername.setBounds(100, 11, 324, 20);
		txtUsername.setSelectionStart(5);
		contentPanel.add(txtUsername);
		txtUsername.setColumns(10);
		
		txtPassword = new JPasswordField();
		txtPassword.setText("root");
		txtPassword.setColumns(10);
		txtPassword.setBounds(100, 42, 324, 20);
		contentPanel.add(txtPassword);
		
		txtHostname = new JTextField();
		txtHostname.setText("localhost");
		txtHostname.setColumns(10);
		txtHostname.setBounds(100, 73, 324, 20);
		contentPanel.add(txtHostname);
		
		txtPort = new JTextField();
		txtPort.setText("1521");
		txtPort.setColumns(10);
		txtPort.setBounds(100, 104, 324, 20);
		contentPanel.add(txtPort);
		
		txtServiceName = new JTextField();
		txtServiceName.setText("xe");
		txtServiceName.setColumns(10);
		txtServiceName.setBounds(100, 135, 324, 20);
		contentPanel.add(txtServiceName);
		
		JButton btnOK = new JButton("OK");
		btnOK.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				setVisible(false);
			}
		});
		btnOK.setBounds(252, 169, 68, 23);
		btnOK.setActionCommand("OK");
		getContentPane().add(btnOK);
		getRootPane().setDefaultButton(btnOK);
		
		JButton btnCancel = new JButton("Cancel");
		btnCancel.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				txtHostname.setText("");
				txtPassword.setText("");
				txtPort.setText("");
				txtServiceName.setText("");
				txtUsername.setText("");
//				setVisible(false);
				dispose();
			}
		});
		btnCancel.setBounds(330, 169, 94, 23);
		btnCancel.setActionCommand("Cancel");
		getContentPane().add(btnCancel);
		
		addWindowListener(new WindowAdapter() {
            @Override
            public void windowClosing(WindowEvent arg0) {
                txtHostname.setText("");
                txtPassword.setText("");
                txtPort.setText("");
                txtServiceName.setText("");
                txtUsername.setText("");
//                setVisible(false);
                dispose();
            }
        });
	}


}
